# -*- coding: utf-8 -*-
"""
Created on Tue Dec 08 11:03:57 2015

@author: liliangrong
"""
def get_img_list(words):
    import urllib
    #import urllib2
    import re
    headers = { 'User-Agent' : 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'  ,
                        'Referer':'http://image.baidu.com/' }      
    url = "http://image.baidu.com/search/index?tn=baiduimage&ipn=r&ct=201326592&cl=2&lm=-1&st=-1&fm=index&fr=&sf=1&fmq=&pv=&ic=0&nc=1&z=&se=1&showtab=0&fb=0&width=&height=&face=0&istype=2&ie=utf-8&word=%s&oq=%s&rsp=-1" % (words,words)
    resp = urllib.urlopen(url,None,headers)
    
    img_lists = resp.read()
    #return img_lists
    
    pattern = re.compile('<img .*? class="main_img img-hover" src="(.*?)">',re.S)
    
    img_list = re.findall(pattern,img_lists)
    
    for i in img_list:
        return i

if __name__ == "__main__":
    import time
    start = time.clock()
    words = "python"
    r = get_img_list(words)
    print r
    """
    with open("img_list.html","w") as f:
        f.write(r)
    f.closed
    """
    end = time.clock()
    print "Run time is %f s" % (end - start)